package mappers;

import handler.DatabaseConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Club;

public class ClubMapper {

    private static ClubMapper instance;
    //Denne ArrayList skal indeholde alle klubber i databasen.
    private ArrayList<Club> clubs;

    private ClubMapper() {
        clubs = new ArrayList<>();
        clubs = getTable();
    }

    public ArrayList<Club> getTable() {

        try {
            // Henter statementet fra den åbne forbindelse i MapperHandleren.
            Statement stmt = DatabaseConnection.getDbInstance().getConnection().createStatement();

            // Henter hele tabellen ind i et resultset.
            String SQL = "Select * from club";
            ResultSet rs = stmt.executeQuery(SQL);

            // Løber tabellen igennem og opretter klubobjekter løbende.
            while (rs.next()) {
                int id = rs.getInt("clubid");
                int tStanding = rs.getInt("standing");
                int division = rs.getInt("division");
                String clubName = rs.getString("clubname");
                int points = rs.getInt("points");
                String stadium = rs.getString("stadium");
                String coach = rs.getString("coach");

                // Alle klubber fra databasen fyldes ind i ArrayListen.
                clubs.add(new Club(id, tStanding, division, clubName, points, stadium, coach));

            }

            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            System.out.println("SQL Fejl: " + ex.getMessage());

        }

        // Returnerer ArrayListen.
        return clubs;

    }

    public ArrayList<Club> getClubs() {
        if (clubs == null) {
            getTable();
        }
        return clubs;
    }

    public ArrayList<Club> getClubs(int division) {
        if (clubs == null) {
            getTable();
        }
        ArrayList<Club> divClubs = new ArrayList<>();
        for (int i = 0; i < clubs.size(); i++) {
            if (clubs.get(i).getDivision() == division) {
                divClubs.add(clubs.get(i));
            }

        }
        return divClubs;
    }

    public Club getClub(int id) {

        //Uanset hvad findes den id som der ledes efter, den trækkes nemlig op 
        //af en entitetsdatabase med primærnøgler, og id'en er primærnøglen.
        Club club = null;

        for (int i = 0; i < clubs.size(); i++) {
            // clubben med den indtastede id findes.
            if (clubs.get(i).getId() == id) {
                club = clubs.get(i);
            }

        }

        return club;
    }

    public static ClubMapper getInstance() {
        if (instance == null) {
            instance = new ClubMapper();
        }
        return instance;
    }

    public void addPoints(int points, Club club) {
        try {
            Statement stmt = DatabaseConnection.getDbInstance().getConnection().createStatement();

            int clubId = club.getId();
            String SQL = "select points from club where clubid = " + clubId;
            ResultSet rs = stmt.executeQuery(SQL);

            rs.next();
            int prevPoints = rs.getInt("points");

            SQL = "update club set points =" + (prevPoints + points) + " where clubid = " + clubId;

            stmt.execute(SQL);

            stmt.close();
            rs.close();
        } catch (SQLException ex) {
            System.out.println("SQL FEJL: " + ex.getMessage());
        }
    }
}
